package com.litxuf.logic.admin.manager;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.litxuf.logic.admin.dao.UserAdminDAO;
import com.litxuf.logic.admin.dao.UserPowerDAO;
import com.litxuf.logic.admin.dao.model.AdminColumn;
import com.litxuf.logic.admin.dao.model.UserAdmin;
import com.litxuf.logic.admin.dao.model.UserPower;
import com.litxuf.logic.core.ManagerFactory;
import com.litxuf.util.StringTools;
import com.yeqiangwei.util.FormatDateTime;

public class UserAdminManager {
	Logger log = Logger.getLogger(this.getClass());

	UserAdminDAO adminDAO;
	UserPowerDAO powerDAO;

	public void updateLoginTime(int id) {

		UserAdmin user = findByUserId(id);
		if (user != null) {
			user.setLogintime(FormatDateTime
					.formatDateTime("yyyy-MM-dd HH:mm:ss"));
			adminDAO.update(user);
		}
	}

	public UserAdmin findByUserNameAndPwd(String userName, String pwd) {
		if (StringUtils.isBlank(userName) || StringUtils.isBlank(pwd)) {
			return null;
		}
		return adminDAO.findByUserNameAndPwd(userName, pwd);
	}

	public String save(UserAdmin item) {

		return adminDAO.save(item);
	}

	/**
	 * 删除用户 made in 2010-1-5下午11:50:30
	 * 
	 * @param id
	 * 
	 */
	public void deleteUser(String id) {
		adminDAO.deleUser(Integer.parseInt(id));
	}

	/**
	 * 修改密码 made in 2010-1-5下午11:49:01
	 * 
	 * @param id
	 * @param pwd
	 * 
	 */
	public void updatePwd(int id, String pwd) {

		UserAdmin user = findByUserId(id);
		if (user != null) {
			user.setPassword(pwd);
			adminDAO.update(user);
			log.info("用户:" + id + ":密码修改成功");
		}
	}

	/**
	 * 更新用户信息 made by litxuf Dec 8, 2010-3:14:44 PM
	 * 
	 * @param user
	 */

	public void updateUser(UserAdmin user) {

		adminDAO.update(user);

	}

	public UserAdmin findByUserId(int userId) {
		return adminDAO.findByUserId(userId);
	}

	public UserAdmin findUserAdminByUserName(String userName) {

		List ls = adminDAO.findByUserName(userName);
		if (ls != null && ls.size() > 0) {
			return (UserAdmin) ls.get(0);
		}
		return null;
	}

	public List findAllBasicUser() {

		return adminDAO.findAllBasicUser();

	}

	public UserAdminDAO getAdminDAO() {
		return adminDAO;
	}

	public void setAdminDAO(UserAdminDAO adminDAO) {
		this.adminDAO = adminDAO;
	}

	public static void main(String[] args) {
		UserAdminManager m = ManagerFactory.getFactory().getAdminManager();
		m.updatePwd(1, "000000");
	}

	/**
	 * 查询管理员的栏目权限 供栏目修改时使用 made in 2010-3-28下午07:08:35
	 * 
	 * @param userId
	 * @return
	 * 
	 */
	public List findUserPower2AdminColum(String userId) {

		List ls = powerDAO.findPowerByUserIdAndType(userId, "2");

		String codes[] = null;

		if (ls != null && ls.size() > 0) {
			codes = new String[ls.size()];
			for (int j = 0; j < ls.size(); j++) {
				UserPower up = (UserPower) ls.get(j);
				codes[j] = up.getPowerCode();
			}

		}

		List column = powerDAO.findAdminColumn();

		for (int x = 0; codes != null && x < column.size(); x++) {
			AdminColumn code = (AdminColumn) column.get(x);
			code.setCheckedOrNot(StringTools.isContain(code.getColumnCode(),
					codes));

		}

		return column;
	}

	public void saveAreaPowerList(List ls, String userId, String powerType) {
		// 1.先删除原来的记录

		powerDAO.deleteByUserId(userId, powerType);

		// 2.添加新记录

		for (int x = 0; ls != null && x < ls.size(); x++) {
			UserPower up = (UserPower) ls.get(x);
			powerDAO.save(up);
		}

	}

	/**
	 * 查找用户权限 made in 2010-3-28下午07:13:07
	 * 
	 * @param userId
	 * @param powerType
	 * @return
	 * 
	 */
	public List findUserPower2List(String userId, String powerType) {

		return powerDAO.findPowerByUserIdAndType(userId, powerType);

	}

	/**
	 * 用户权限 返回数组 made in 2010-3-28下午10:53:13
	 * 
	 * @param userId
	 * @param powerType
	 * @return
	 * 
	 */
	public String[] findUserPower2Array(String userId, String powerType) {

		List ls = powerDAO.findPowerByUserIdAndType(userId, powerType);

		String codes[] = null;

		if (ls != null && ls.size() > 0) {
			codes = new String[ls.size()];
			for (int j = 0; j < ls.size(); j++) {
				UserPower up = (UserPower) ls.get(j);
				codes[j] = up.getPowerCode();
			}

		}

		return codes;

	}

	public void setPowerDAO(UserPowerDAO powerDAO) {
		this.powerDAO = powerDAO;
	}

}
